﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SAPbouiCOM;

namespace BoAddonBiz_FreightForwarder.Approval
{
    class B1Form_ApprovalStages:SAPBoAddon.B1Form
    {
        #region Constructor
        public B1Form_ApprovalStages()
        {
            this.frm_ItemEvent += new SAPBoAddon.IB1FormEvents.B1ItemEventEventHandler(B1Form_ApprovalStages_frm_ItemEvent);
            this.frm_MenuEvent += new SAPBoAddon.IB1FormEvents.B1MenuEventEventHandler(B1Form_ApprovalStages_frm_MenuEvent);
        }
        #endregion
        #region override
        protected override bool BeforeSaveCheckFormData()
        {
            return true;
        }

        protected override void FormEditModeChange(SAPBoAddon.frmItemEditMode frmEditMode)
        {
            switch (frmEditMode)
            { 
                case SAPBoAddon.frmItemEditMode.fe_AddMode:
                    this.Matrix_AutoAddLine();
                    break;
            }
        }

        protected override void FormInfoSetting(SAPBoAddon.B1FormSetting FormSetting)
        {
            this.FormSetting.FormType = B1FormTypes.ado_AVA_FF_ApprovalStages;
            this.FormSetting.FormFileName = "AVA_ApprovalStagesForm.srf";

            FormSetting.AutoGetObjectNextNumber = false;
            #region MatrixSetting
            FormSetting.MatrixSetting.MtxUID = "mtx_0";
            FormSetting.MatrixSetting.AutoAddNewLine = true;
            FormSetting.MatrixSetting.NotAllowNoLineSave = false;
            FormSetting.MatrixSetting.AutoLineKeyCol = "col_1";
            FormSetting.MatrixSetting.BeforeSaveFixDbDataSourceData = false;
            FormSetting.MatrixSetting.BeforeSaveDeleteNoValueLine = true;
            FormSetting.MatrixSetting.TableKeyField = "LineId";
            FormSetting.MatrixSetting.TableVisOrder = "LineId";
            FormSetting.MatrixSetting.PrimaryLineCol = "col_f";
            FormSetting.MatrixSetting.TableName = "@AVA_WST1";
            FormSetting.MatrixSetting.Add();
            #endregion
        }

        protected override void InitializeForm(SAPBoAddon.B1FormInitializePar InitPar)
        {
            Utils.InitialMatrixComboBox(SBOCompany, this.CurrentForm.Items.Item("mtx_0").Specific,"col_1", "UserID", "U_Name", "OUSR");
            Utils.InitialMatrixComboBox(SBOCompany, this.CurrentForm.Items.Item("mtx_0").Specific, "col_2", "Code", "Name", "OUDP",null,"",null,true);
        }
        #endregion
        #region Events
        void B1Form_ApprovalStages_frm_MenuEvent(SAPBoAddon.B1AddonBase.B1MenuEvent pVal, ref bool BubbleEvent)
        {
            if (pVal.BeforeAction)
            {
                if (pVal.MenuUID == "1283")//删除
                {
                    Delete(ref BubbleEvent);
                }
            }
        }
        void B1Form_ApprovalStages_frm_ItemEvent(SAPBoAddon.B1AddonBase.B1ItemEvent pVal, ref bool BubbleEvent)
        {
            if (pVal.BeforeAction)
            {
                if (pVal.ItemUID == "1" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                {
                    SaveBeforeCheck(pVal.FormUID, ref BubbleEvent);
                }
            }
            else
            {
                if (pVal.ItemUID == "mtx_0" && pVal.ColUID == "col_1" && pVal.ItemChanged)
                {
                    GetDepartment(pVal.Row);   
                }
                if (pVal.ItemUID == "mtx_0" && pVal.ColUID == "col_1" && (pVal.ItemChanged || pVal.EventType == BoEventTypes.et_COMBO_SELECT))
                {
                    SAPBoAddon.B1Assistant.Matrix.AutoAddNewLine(this.CurrentForm, "mtx_0", "col_f", "@AVA_WST1",-1, "Lineid", "Lineid", true);
                }
            }
        }
        #endregion
        #region method
        /// <summary>
        /// 保存之前检查Code、Name
        /// </summary>
        /// <param name="FormUID"></param>
        /// <param name="BubbleEvent"></param>
        private void SaveBeforeCheck(string FormUID, ref bool BubbleEvent)
        {
            SAPbouiCOM.Form oForm;
            SAPbouiCOM.DBDataSource dbds;
            SAPbobsCOM.Recordset ors;
            string ApprovalCode = string.Empty;

            oForm = SBOApp.Forms.Item(FormUID);
            dbds = oForm.DataSources.DBDataSources.Item("@AVA_OWST");
            ors = SBOCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);
            if (oForm.Mode != BoFormMode.fm_ADD_MODE && oForm.Mode != BoFormMode.fm_UPDATE_MODE && oForm.Mode != BoFormMode.fm_EDIT_MODE) return;

            ApprovalCode = dbds.GetValue("U_AppCode", dbds.Offset).Trim();
            if (string.IsNullOrEmpty(ApprovalCode))
            {
                BubbleEvent = false;
                SBOApp.MessageBox("名称不能为空，请检查！");
                return;
            }
            if (oForm.Mode == BoFormMode.fm_ADD_MODE)
            {
                ors.DoQuery("SELECT COUNT(1) FROM [@AVA_OWST] Where U_AppCode='" + ApprovalCode + "'");
                if (ors.Fields.Item(0).Value > 0)
                {
                    BubbleEvent = false;
                    SBOApp.MessageBox("此名称：" + ApprovalCode + " 已存在系统中，请检查！");
                    return;
                }
            }

            ors.DoQuery("SELECT Autokey FROM ONNM  Where ObjectCode = 'AVA_ApprovalStages'");
            dbds.SetValue("Code", dbds.Offset, ors.Fields.Item("Autokey").Value);
            dbds.SetValue("Name", dbds.Offset, ors.Fields.Item("Autokey").Value);

        }
        /// <summary>
        /// 获取部门信息
        /// </summary>
        /// <param name="row"></param>
        private void GetDepartment(int row)
        { 
            SAPbouiCOM.Matrix oMtx = CurrentForm.Items.Item("mtx_0").Specific;
            SAPbobsCOM.Recordset ors = SBOCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);
            SAPbouiCOM.ComboBox oComboBox = oMtx.GetCellSpecific("col_1", row);
            ors.DoQuery("SELECT Department FROM OUSR Where UserID = " + oComboBox.Selected.Value);
            if (ors.RecordCount > 0)
            {
                oComboBox = oMtx.GetCellSpecific("col_2", row);
                string DeptCode = ors.Fields.Item("Department").Value.ToString();
                oComboBox.SelectExclusive(DeptCode, BoSearchKey.psk_ByValue);
            }
        }
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="BubbleEvent"></param>
        private void Delete(ref bool BubbleEvent)
        {
            SAPbobsCOM.Recordset ors = SBOCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);
            ors.DoQuery("select COUNT(U_AppCode) from [@AVA_WTM2] Where U_AppCode = '" + SAPBoAddon.B1Assistant.Item.GetValue(CurrentForm, "4").ToString() + "'");
            if (ors.Fields.Item(0).Value > 0)
            {
                SBOApp.MessageBox("此审批阶段已被使用，不能直接删除。");
                BubbleEvent = false;
            }
        }
        #endregion
    }
}
